package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117685718";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCE4j/IEVSfjnZv7mYqA8RsKpDoPiasyXcvnaXawnNgoP1YNJ/jzrazPmAMlM63hQxbdqKJMle+Zm6tMCXq2xuSDL52AeEFVBueRE+Z+cHgtO8wg4Nwo27x5HlO4MlTNu4ZKA/466MmSHiCTIWJVA/a+qca85ubjgQv1NT7ZUNKzZGLbz7Lew2H24RbJ5n4YSXiqPshMe4AblNirXjXLEgdx2GlILWv3+iv2AcqWBeV/g6xeRRxgS82aV/dh6/29kjrWPQQJNlI1WLGLnqkB/JhzwaAzGMCo9QxPmhpRH3UdIywu9mzEWa+qCkpJx9Apa335pt/cL3IyW3L+A74sqabAgMBAAECggEAA6O6wX0QB0q8nbY24CG4SIol6+Z/4q2l8QztVP+Mb3IZXkgatP+da21i7hVJUCd5viuSqhVR5+ogT+gIsdUMfUSSFuNYSXATTZNrZrk7gS8Teg2wFO61BDUQyYNIoFnZzaCWR2gU0OCdRFxaL/e4c3P4DbnsYidSoAjBSJ1uaWcL2zQyXR7sr+3JK0kRH/x9ncM0UYngowKbhj4oO+MUtzMLkE6/kNsh7d2AYzdKcZNHMamD+Gcx8xVExvfX8QBaS5L2j0OqKX8oQmLvCIn1StTLLWHhHUo1LHvDV+YosfjVdsXnhZh+3TnIUYaSN0Zs3ePNqbuOO6auCw4N2g2k+QKBgQDGSYtIr9ecQxl0E0oSmh6U1o4semSOi47FiNyAzfXnK980hT24TrUYg51qVsC3bY0Z9SgAX09a+uOxgBtNwS+uoES0y6AmZ8vCN7CmSS9fa9yM7upyXne4AzixHJx3ewGvUMm/sK2w7V3yaCsIGzJlyRJqP7Q4zHxVSgOHBnsFvQKBgQCrj3eiD3mKP2npVfNu+Q5IRpqU7YNUNKj/ZXwvghufg7JxY3F7y78GObdeHV7ygOeBOSYz4vbgmcMG5a2SuPFYjoAwsCnKPtW/tmWqyRdZqH49XBt8qx1asM/5PMEKd3c5Ru9Zi7p/zg9go8OB/Uh56Wu3hOAJTlGz/9+UVrFHNwKBgQDFI/2NCPbu729xDnSIxh49kIvOAOnVJz+NeaLxlNQ1QFMsnoRcqDQ46TIqtZHpTMwK0MkyhZAaPnL+2QVMaxQDJGjq78CMLopQ0Jma2gx1bIHcxPTnPLD+jIVi4bVWvT9RlJrhMMGftH/BMHwIFTIoLJ9Yb3m5NuUuzuVu66yIaQKBgDewuKr4zwFbmGvQJNePLV203ZUejJUXCwzsyqU5XPIo9BHur/X5q6a/sZBxhFLAGWZPQOFy+aTrBO4GPJP2SRHwIBit9sLaJtS2xsXrxRKrqV8NuTZ87Wh1IO3NFPTq1t7W8fdlbkLyhvdd8acOqyo35WmLTpZ2v/roQpGKmr19AoGBAIaydRbgTTUSRS4215QRDKqzNNuiNl9Xq8cWGK64jTsygAhlFosxOVaQWiwbriBWx8dlb/HzydW1qSUVN/4YgithT/E2I/KCW9KRhIoDsPZ7VXUykPrb+NCTLyAwSOWM9ssxlDT90kF42v+F+yjNm+AlDZvv9xxCJWyyEdUOE2fm";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqnrb9Y7FaJTNJh9OVXOn56585GuiuxX9g9hm1QWDMWQLV2QXONdjeYm9lhrYuHNLtJ9Hu9pyVCYgLKHuZ4jUZ6ecC4dJoXcbpgqg7krItvpSbbLkb4L5t/aIV/YIUHVw3LN/Np7+JNWFdDN72HoMO1mmJnFRieOR+J894DNtF8xZtJZVl8sxChniDxbiQcY/TZ9LNAqW5qZVRf7+BqZczNxp56nLYXh3ELi4HYAJIoWCSYk3GRHGG+wIZ14UqfM9EQMnaiWZ1yrUAitQVJYgPJH44zx+wwxRrKCRhyLGtgZBfqzrUBqUtjv9/P2Tg6hoKuFKUnka81N2AuU9Ji3UEQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://hdikey.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://order.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"30s\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
